import {Component} from 'react'
import {Breadcrumb} from "antd";
import {HomeOutlined} from "@ant-design/icons";
import {inject,observer} from "mobx-react";
import {Link,withRouter} from 'react-router-dom'
@inject('login')
@observer
class Newmianbao extends Component{
    constructor(props) {
        super(props);
        this.state = {
            menulist: [],
            xie:[]
        }
    }
    changeMenuList() {
        const arr = this.props.login.gongnengInfo.reduce(function f(pre, item) {
            pre.push(item)
            if (item.childrens && item.childrens.length > 0) item.childrens.reduce(f, pre);
            return pre;
        }, []).map((item) => {
            item.childrens = []
            return item
        })

        this.setState({
            menulist: arr
        }, function () {
            this.buildBread()
        })
    }
    buildBread(){
        this.setState({
            xie:[]
        },function (){
            let path = this.props.history.location.pathname
            let cmenu = this.state.menulist.find((item)=>{
                return item.menuUrl === path
            })
            if (cmenu&&cmenu.menuId){
                this.buildBreadItemByMenuId(cmenu.menuId)
            }
        })
    }
    buildBreadItemByMenuId= (menuId) =>{
    let  menu =this.state.menulist.find((item)=>{
        return item.menuId === menuId
    })
        if(menu) {
            this.setState({
                xie: [
                    (<Breadcrumb.Item key={menu.menuUrl}>
                        <Link to={menu.menuUrl}>
                            {menu.menuName}
                        </Link>
                    </Breadcrumb.Item>),...this.state.xie]
            },function () {
                if (menu.pId !== 0) {
                    this.buildBreadItemByMenuId(menu.pId)
                }
            })
        }
    }
    componentWillReceiveProps(){
        this.buildBread()
    }
    componentWillMount() {
     this.changeMenuList()
    }

    render() {
        return (
            <div>
                <Breadcrumb>
                    <Breadcrumb.Item>
                        <span><a href='/index'><HomeOutlined /></a></span>
                    </Breadcrumb.Item>
                    {this.state.xie}
                </Breadcrumb>
            </div>
        )
    }
}
export default withRouter(Newmianbao)